#!/usr/bin/env bash
# Copyright 1999-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

source "${PORTAGE_BIN_PATH}"/isolated-functions.sh || exit 1

if ! ___eapi_has_prefix_variables; then
	ED=${D}
fi

args=()
got_owner=
for arg; do
	if [[ ${arg} == -* ]]; then
		args+=( "${arg}" )
	elif [[ ! ${got_owner} ]]; then
		# the first non-option is the owner and must not be prefixed
		got_owner=1
		args+=( "${arg}" )
	else
		args+=( "${ED%/}/${arg#/}" )
		# remove the QA warning after 2024-12-31
		if [[ ${arg:0:1} != / ]]; then
			eqawarn "QA Notice: ${0##*/}: Path '${arg}' does not start with '/'."
			eqawarn "This is unsupported. Add a slash for a path in \${ED},"
			eqawarn "or use 'chown' for a path relative to the working dir."
		fi
	fi
done

chown "${args[@]}"
ret=$?

[[ ${ret} -ne 0 ]] && __helpers_die "${0##*/} failed"
exit ${ret}
